<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Listingmap extends MX_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 * 	- or -
	 * 		http://example.com/index.php/welcome/index
	 * 	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function Listingmap() {
		parent::__construct();
		$this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		//check is logged in
		$this->load->model('languagemodel');
		$this->load->model('projects/mdl_projectsmodel');
		$this->language = $this->languagemodel->is_language_using();
		$this->load->helper('MY_url_helper');
		$this->load->helper('utf8');
		$this->load->helper('text');
		
		//right side banner
		$this->load->model('banners/bannermodel');
	}

	public function index() {
		$this->lang->load('listing/map', $this->language);

		//SEO
		$data['title'] = $this->lang->line('listing_map_text_title');
		$data['description'] = $this->lang->line('listing_map_text_description');
		$data['keywords'] = $this->lang->line('listing_map_text_keywords');

		$data['module'] = "listingmap";
		$data['view_file'] = "listingmap";

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_heading'),
			'href'      => base_url() . 'listing-map.html?localorint=0&propertyfor=sale'
		);
		
		$cities = $this->input->get('cities');
		$valToArr = array();
		if($this->input->get('cities')) {
			if (!is_array($this->input->get('cities'))) {
				$valToArr = explode(',', $this->input->get('cities'));
			} else {
				$valToArr = $this->input->get('cities');
			}
		}
		$data['cities'] = $valToArr;
		
		$options['propertytype'] = $this->input->get('propertytype');
		$data['propertytype'] = $options['propertytype'];
		if($options['propertytype'] != "") {
			$options['propertytype'] = $this->input->get('propertytype');
		} else {
			$options['propertytype'] = NULL;
		}
		
		//get banner for right side
		$data['banner'] = $this->bannermodel->getBanners('Result', $this->config->item('market'), $this->config->item('config_language_id'));

		echo Modules::run('template/default_template', $data);
	}

	function get_with_limit($limit, $offset, $order_by) {
		$this->load->model('mdl_perfectcontroller');
		$query = $this->mdl_perfectcontroller->get_with_limit($limit, $offset, $order_by);
		return $query;
	}

	function xmlListing() {
		//1. Start XML file, create parent node
		$this->load->helper('xml');
		$this->load->model('mdl_listingmapmodel');
		$options = array();

		$options = array();
		
		$options['language'] = $this->config->item('config_language_id');
		$options['market'] = $this->config->item('market');

		//refer to Int listing search
		$localorInt = $this->input->get('localorint');
		if($localorInt) {
			$options['localorInt'] = $localorInt;
		} else {
			$options['localorInt'] = 0;
		}
		$data['localorInt'] = $localorInt;
		$options['country'] = $this->lookupmodel->getCountryId($this->config->item('market'));
		 
		$cities = $this->input->get('cities');
		$valToArr = array();
		if($this->input->get('cities')) {
			if (!is_array($this->input->get('cities'))) {
				$valToArr = explode(',', $this->input->get('cities'));
			} else {
				$valToArr = $this->input->get('cities');
			}
		}
		$data['cities'] = $valToArr;

		if ($this->helpermodel->check_is_array($cities)) {
			$options['cities'] = implode(',', $this->input->get('cities'));
		} else {
			if($cities != "") {
				$options['cities'] = $this->input->get('cities');
			} else {
				$options['cities'] = NULL;
			}
		}

		$locations = $this->input->get('locations');
		$data['locations'] = $locations;

		if ($this->helpermodel->check_is_array($locations)) {
			$options['locations'] = implode(',', $this->input->get('locations'));
		} else {
			$options['locations'] = NULL;
		}

		$subarea = $this->input->get('chkSubArea');

		if ($this->helpermodel->check_is_array($subarea)) {
			$options['subarea'] = implode(',', $this->input->get('chkSubArea'));
		} else {
			$options['subarea'] = NULL;
		}

		$options['list_type'] = $this->input->get('list_type');
		if($options['list_type'] != "") {
			$options['list_type'] = $this->input->get('list_type');
		} else {
			$options['list_type'] = NULL;
		}

		$options['propertyfor'] = $this->input->get('propertyfor');
		$data['propertyfor'] = $options['propertyfor'];
		if($options['propertyfor'] != "") {
			$options['propertyfor'] = $this->input->get('propertyfor');
		} else {
			$options['propertyfor'] = NULL;
		}

		$options['propertytypeclss'] = $this->input->get('ddlPropertyClss');
		$data['propertytypeclss'] = $options['propertytypeclss'];
		if($options['propertytypeclss'] != "") {
			$options['propertytypeclss'] = $this->input->get('ddlPropertyClss');
		} else {
			$options['propertytypeclss'] = NULL;
		}

		$options['propertytype'] = $this->input->get('propertytype');
		if($options['propertytype'] != "") {
			$options['propertytype'] = $this->input->get('propertytype');
		} else {
			$options['propertytype'] = NULL;
		}

		$options['price_min'] = $this->input->get('price_min');
		$data['price_min'] = $options['price_min'];
		if($options['price_min'] != "") {
			$options['price_min'] = $this->input->get('price_min');
		} else {
			$options['price_min'] = NULL;
		}

		$options['price_max'] = $this->input->get('price_max');
		$data['price_max'] = $options['price_max'];
		if($options['price_max'] != "") {
			$options['price_max'] = $this->input->get('price_max');
		} else {
			$options['price_max'] = NULL;
		}

		$options['size_min'] = $this->input->get('size_min');
		$data['size_min'] = $options['size_min'];
		if($options['size_min'] != "") {
			$options['size_min'] = $this->input->get('size_min');
		} else {
			$options['size_min'] = NULL;
		}

		$options['size_max'] = $this->input->get('size_max');
		$data['size_max'] = $options['size_max'];
		if($options['size_max'] != "") {
			$options['size_max'] = $this->input->get('size_max');
		} else {
			$options['size_max'] = NULL;
		}

		$options['bedroom_min'] = $this->input->get('bedroom_min');
		$data['bedroom_min'] = $options['bedroom_min'];
		if($options['bedroom_min'] != "") {
			$options['bedroom_min'] = $this->input->get('bedroom_min');
		} else {
			$options['bedroom_min'] = NULL;
		}
		
		$options['bedroom_max'] = $this->input->get('bedroom_max');
		$data['bedroom_max'] = $options['bedroom_max'];
		if($options['bedroom_max'] != "") {
			$options['bedroom_max'] = $this->input->get('bedroom_max');
		} else {
			$options['bedroom_max'] = NULL;
		}

		$options['bathroom_min'] = $this->input->get('bathroom_min');
		$data['bathroom_min'] = $options['bathroom_min'];
		if($options['bathroom_min'] != "") {
			$options['bathroom_min'] = $this->input->get('bathroom_min');
		} else {
			$options['bathroom_min'] = NULL;
		}
		
		$options['bathroom_max'] = $this->input->get('bathroom_max');
		$data['bathroom_max'] = $options['bathroom_max'];
		if($options['bathroom_max'] != "") {
			$options['bathroom_max'] = $this->input->get('bathroom_max');
		} else {
			$options['bathroom_max'] = NULL;
		}

		$options['keyword'] = $this->input->get('keyword');
		$data['keyword'] = $options['keyword'];
		if($options['keyword'] != "") {
			$options['keyword'] = $this->input->get('keyword');
		} else {
			$options['keyword'] = NULL;
		}

		$options['listing_id'] = NULL;
		//Features
		$features = $this->input->get('feature_category');

		if ($this->helpermodel->check_is_array($features)) {
			$options['features'] = implode(',', $features);
		} else {
			$options['features'] = NULL;
		}

		//Facilities
		$facilities = $this->input->get('facilities_category');

		if ($this->helpermodel->check_is_array($facilities)) {
			$options['facilities'] = implode(',', $facilities);
		} else {
			$options['facilities'] = NULL;
		}
		
		//tenure
		$options['tenu'] = $this->input->get('tenure');
		$data['tenu'] = $options['tenu'];
		if($options['tenu'] != "") {
			$options['tenu'] = $this->input->get('tenure');
		} else {
			$options['tenu'] = NULL;
		}

		if ($this->input->get('sort')) {
			$sort = $this->input->get('sort');
			$data['sort'] = $sort;
			if ($sort == 'date-listed') {
				$options['orderby'] = "ListedOnUtc DESC";
			}

			if ($sort == 'date-listed-old-to-new') {
				$options['orderby'] = "ListedOnUtc ASC";
			}

			if ($sort == 'price-low-to-high') {
				$options['orderby'] = "Price_Min ASC";
			}

			if ($sort == 'price-high-to-low') {
				$options['orderby'] = "Price_Min DESC";
			}

			if ($sort == 'size-low-to-high') {
				$options['orderby'] = "Size_Min ASC";
			}

			if ($sort == 'size-high-to-low') {
				$options['orderby'] = "Size_Min DESC";
			}
		} else {
			$options['orderby'] = 'ListedOnUtc DESC';
			$data['sort'] = 'date-listed';
		}

		if ($this->input->get('page')) {
			$options['page'] = $this->input->get('page');
		}
		else{
			$options['page'] = 1;
		}

		if($this->input->get('limit')) {
			$limit = $this->input->get('limit');
		} else {
			$limit = PER_PAGE;
		}

		$data['limit'] = $limit;

		$options['offset'] = NULL;
		$options['limit'] = NULL;

		$query = $this->mdl_listingmapmodel->getProjectListing($options);
		
		$data['mapListing'] = array();
		$this->load->model('imagemodel');

		foreach ($query as $row) {
			$media = $this->mdl_listingmapmodel->get_media($row['Listing_ID'], 'Cover', 1);
			if($media) {
				$image = $this->imagemodel->resize($media->FileName . $media->FileExtension, 100, 74, HTTP_PROJECT_LISTING_IMAGE . $row['Listing_ID'] . '/', $this->config->item('HTTP_LISTING_MEDIA_IMAGE') . $row['Listing_ID'] . '/');
			} else {
				//default image
				$image = $this->config->item('HTTP_IMAGE') . 'tmp/search-map-no-image .png';
			}
			
			$locality = "";
			//check subarea
			if (!empty($row['Subarea'])) {
				$locality .= $row['Subarea'] . ", ";
			}
			
			//check location
			if (!empty($row['Location'])) {
				$locality .= $row['Location'] . ", ";
			}
			
			//check city
			if (!empty($row['City'])) {
				$locality .= $row['City'] . ", ";
			}
				
			$data['mapListing'][] = array(
				'latitude'		=> $row['Latitude'],
				'longitude'		=> $row['Longitude'],
				'projectname'	=> $row['ProjectName'],
				'streetname'	=> $locality,
				'propertyfor'	=> $row['PropertyFor'],
				'floorarea'		=> $row['Size_Min'],
				'askingprice'	=> $row['Price_Min'],
				'listing_id'	=> $row['Link'],
				'thumbnail'		=> $image,
				'bedroom'		=> $row['Bedroom'],
				'bathroom'		=> $row['Bathroom']
			);
		}

		$json = array();
		 
		echo json_encode($data['mapListing']);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */